<?php
/**
 * CustomersController.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-14 04:36:37
 * @modified    2023-02-14 04:36:37
 *
 */

namespace App\Http\Controllers;

use App\Repositories\CustomerRepo;
use App\Repositories\ProductRepo;
use Illuminate\Http\Request;

class CustomersController extends BaseController
{
    /**
     * @OA\Get(
     *     path="/customers/params",
     *     tags={"Customer"},
     *     summary="获取客户参数列表",
     *     description="获取客户参数列表",
     *     operationId="customer_params",
     *     security={{"bearerAuth":{}}},
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function params(Request $request)
    {
        $params = CustomerRepo::params();
        return message("获取成功", true, $params);
    }

    /**
     * @OA\Get(
     *     path="/customers",
     *     tags={"Customer"},
     *     summary="获取客户列表",
     *     description="获取客户列表",
     *     operationId="customer_index",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="keyword", in="query", description="搜索关键字", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="department_id", in="query", description="服务部门ID", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="owner_id", in="query", description="负责人ID", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="status", in="query", description="状态", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="page", in="query", description="当前页数", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="limit", in="query", description="每页数量(默认10)", required=false, @OA\Schema(default=10,type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function index(Request $request)
    {
        $customers = CustomerRepo::getList();
        return list_message("获取成功", true, $customers);
    }

    /**
     * @OA\Get(
     *     path="/customers/{id}/users",
     *     tags={"Customer"},
     *     summary="获取客户所有联系人",
     *     description="获取客户所有联系人",
     *     operationId="customer_users_show",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="客户ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function users(Request $request, $id)
    {
        $users = CustomerRepo::users($id);
        return message("获取成功", true, $users);
    }

    /**
     * @OA\Get(
     *     path="/customers/{id}/products",
     *     tags={"Customer"},
     *     summary="获取客户所有产品",
     *     description="获取客户所有产品",
     *     operationId="customer_products_show",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="客户ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\Parameter(name="product_name", in="query", description="产品名称", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function products(Request $request, $id)
    {
        $products = CustomerRepo::products($id);
        return message("获取成功", true, $products);
    }

    /**
     * @OA\Post(
     *     path="/customers",
     *     tags={"Customer"},
     *     summary="添加客户",
     *     description="添加客户",
     *     operationId="customer_store",
     *     security={{"bearerAuth":{}}},
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"parent_id", "name", "short_name", "entry_type_name", "province_id", "city_id", "county_id", "address", "industry_name", "customer_type_name", "department_id", "tag_name", "safe_level", "source", "owner_id", "status", "contact_at", "service_limit"},
     *              @OA\Property(property="parent_id", type="integer", example="上级单位ID", description=""),
     *              @OA\Property(property="event_id", type="integer", example="事件ID(非必填)", description=""),
     *              @OA\Property(property="name", type="string", example="姓名", description=""),
     *              @OA\Property(property="short_name", type="string", example="简称", description=""),
     *              @OA\Property(property="entry_type_name", type="string", example="类型名称(客户|供应商|分销商|合作伙伴|...)", description=""),
     *              @OA\Property(property="province_id", type="integer", example="省份ID", description=""),
     *              @OA\Property(property="city_id", type="integer", example="城市ID", description=""),
     *              @OA\Property(property="county_id", type="integer", example="区县ID", description=""),
     *              @OA\Property(property="address", type="string", example="详细地址", description=""),
     *              @OA\Property(property="industry_name", type="string", example="所在行业(新能源|汽车)", description=""),
     *              @OA\Property(property="customer_type_name", type="string", example="客户性质(国企，民营、外企、合资)", description=""),
     *              @OA\Property(property="department_id", type="integer", example="服务网点ID", description=""),
     *              @OA\Property(property="tag_name", type="string", example="客户标签(一般|重要|核心|VIP)", description=""),
     *              @OA\Property(property="safe_level", type="string", example="安全等级(A类|B类|C类)", description=""),
     *              @OA\Property(property="source", type="string", example="客户来源(企业开拓|主动联系|客户推荐|企业资源)", description=""),
     *              @OA\Property(property="owner_id", type="integer", example="负责人ID", description="负责人ID"),
     *              @OA\Property(property="contact_at", type="string", example="签约日期", description="签约日期"),
     *              @OA\Property(property="status", type="integer", example="状态(0|1)", description="状态(0|1)"),
     *              @OA\Property(property="contact_name", type="string", example="联系人姓名(保存新客户时必填)", description=""),
     *              @OA\Property(property="contact_phone", type="string", example="联系电话(保存新客户时必填)", description=""),
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function store(Request $request)
    {
        try {
            $customer = CustomerRepo::store(0);
            return message("创建成功", true, $customer);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/customers/{id}/user",
     *     tags={"Customer"},
     *     summary="添加客户联系人(保存联系人到老客户)",
     *     description="添加客户联系人(保存联系人到老客户)",
     *     operationId="customer_store_user",
     *     @OA\Parameter(name="id",in="path",description="客户ID",required=true,@OA\Schema(default="",type="integer")),
     *     security={{"bearerAuth":{}}},
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"event_id", "customer_user_id", "contact_name", "contact_phone", "province_id", "city_id", "county_id", "address"},
     *              @OA\Property(property="event_id", type="integer", example="事件ID", description=""),
     *              @OA\Property(property="customer_user_id", type="integer", example="联系人ID", description=""),
     *              @OA\Property(property="contact_name", type="string", example="联系人姓名", description=""),
     *              @OA\Property(property="contact_phone", type="string", example="联系人电话", description=""),
     *              @OA\Property(property="province_id", type="integer", example="省份ID", description=""),
     *              @OA\Property(property="city_id", type="integer", example="城市ID", description=""),
     *              @OA\Property(property="county_id", type="integer", example="区县ID", description=""),
     *              @OA\Property(property="address", type="string", example="详细地址", description="")
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function store_user(Request $request, $id)
    {
        try {
            $customerUser = CustomerRepo::storeUser($id);
            return message('保存成功', true, $customerUser);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/customers/{id}",
     *     tags={"Customer"},
     *     summary="获取客户详情",
     *     description="获取客户详情",
     *     operationId="customer_show",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="客户ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function show(Request $request, $id)
    {
        try {
            $customer = CustomerRepo::fetch($id);
            return message("获取成功", true, $customer);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Put (
     *     path="/customers/{id}",
     *     tags={"Customer"},
     *     summary="编辑客户",
     *     description="编辑客户",
     *     operationId="customer_update",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="商品ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"parent_id", "name", "short_name", "entry_type_name", "province_id", "city_id", "county_id", "address", "industry_name", "customer_type_name", "department_id", "tag_name", "safe_level", "source", "owner_id", "status", "contact_at", "service_limit"},
     *              @OA\Property(property="parent_id", type="integer", example=0, description="上级单位ID"),
     *              @OA\Property(property="name", type="string", example="", description="姓名"),
     *              @OA\Property(property="short_name", type="string", example="", description="简称"),
     *              @OA\Property(property="entry_type_name", type="string", example="", description="类型名称(客户|供应商|分销商|合作伙伴|...)"),
     *              @OA\Property(property="province_id", type="integer", example=0, description="省份ID"),
     *              @OA\Property(property="city_id", type="integer", example=0, description="城市ID"),
     *              @OA\Property(property="county_id", type="integer", example="", description="区县ID"),
     *              @OA\Property(property="address", type="string", example="", description="详细地址"),
     *              @OA\Property(property="industry_name", type="string", example="", description="所在行业(新能源|汽车)"),
     *              @OA\Property(property="customer_type_name", type="string", example="", description="客户性质(国企，民营、外企、合资)"),
     *              @OA\Property(property="department_id", type="integer", example="", description="服务网点ID"),
     *              @OA\Property(property="tag_name", type="string", example="", description="客户标签(一般|重要|核心|VIP)"),
     *              @OA\Property(property="safe_level", type="string", example="", description="安全等级(A类|B类|C类)"),
     *              @OA\Property(property="source", type="string", example="", description="客户来源(企业开拓|主动联系|客户推荐|企业资源)"),
     *              @OA\Property(property="contact_at", type="string", example="", description="签约日期"),
     *              @OA\Property(property="status", type="integer", example=1, description="状态(0|1)"),
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function update(Request $request, $id)
    {
        try {
            $customer = CustomerRepo::store($id);
            return message("更新成功", true, $customer);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Delete(
     *     path="/customers/{id}",
     *     tags={"Customer"},
     *     summary="删除客户",
     *     description="删除客户",
     *     operationId="customer_delete",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="客户ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function destroy(Request $request, $id)
    {
        try {
            CustomerRepo::destroy($id);
            return message("删除成功", true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Delete(
     *     path="/customers",
     *     tags={"Customer"},
     *     summary="批量删除客户",
     *     description="批量删除客户",
     *     operationId="customer_delete_batch",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="ids", in="query", description="要删除的多个客户档案ID，用逗号分隔，如: (1,2,3)", required=true, @OA\Schema(default="", type="string")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function destroy_batch(Request $request)
    {
        try {
            CustomerRepo::destroy_batch();
            return message("删除成功", true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Put (
     *     path="/customers/{id}/default_user",
     *     tags={"Customer"},
     *     summary="更新客户默认联系人",
     *     description="更新客户默认联系人",
     *     operationId="customer_default_user",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="客户ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\RequestBody(
     *          description="表单数据",
     *          @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(
     *                 type="object",
     *                 required={"customer_user_id"},
     *                 @OA\Property(property="customer_user_id", description="当前客户所属的联系人ID", type="integer"),
     *             )
     *         )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function default_user(Request $request, $id)
    {
        $customerUserId = intval($request->post('customer_user_id') ?? 0);
        try {
            CustomerRepo::setDefaultUser($id, $customerUserId);
            return message("更新成功", true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/customers_export",
     *     tags={"Customer"},
     *     summary="导出客户",
     *     description="导出客户",
     *     operationId="customer_export",
     *     security={{"bearerAuth":{}}},
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function export(Request $request)
    {
        try {
            $url = CustomerRepo::export();
            return message('导出成功', true, ['url' => $url]);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }
}